function closeWindow ()
  {
    window.arguments[1].call(null, null);               
    window.close();
  }

  function doneAuthorizing(oauth_verifier)
  {
    alert("doneAuthorizing: "+oauth_verifier+"\n");
	var aConsoleService = Components.classes["@mozilla.org/consoleservice;1"].
     getService(Components.interfaces.nsIConsoleService);

	aConsoleService.logStringMessage("doneAuthorizing: "+oauth_verifier+"\n");
    window.arguments[1].call(null, oauth_verifier);             
    window.close();
  }
                
  var reporterListener = {

    QueryInterface: function(aIID) {
      if (aIID.equals(Components.interfaces.nsIWebProgressListener)   ||
          aIID.equals(Components.interfaces.nsIWebProgressListener2)  ||
          aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
          aIID.equals(Components.interfaces.nsISupports))
        return this;
      throw Components.results.NS_NOINTERFACE;
    },
    onStateChange: function(/*in nsIWebProgress*/ aWebProgress,
                       /*in nsIRequest*/ aRequest,
                       /*in unsigned long*/ aStateFlags,
                       /*in nsresult*/ aStatus) {
      },
  
    onProgressChange: function(/*in nsIWebProgress*/ aWebProgress,
                          /*in nsIRequest*/ aRequest,
                          /*in long*/ aCurSelfProgress,
                          /*in long */aMaxSelfProgress,
                          /*in long */aCurTotalProgress,
                          /*in long */aMaxTotalProgress) {
      },
  
    onLocationChange: function(/*in nsIWebProgress*/ aWebProgress,
                          /*in nsIRequest*/ aRequest,
                          /*in nsIURI*/ aLocation) {
      //alert("onLocationChange: "+aLocation.spec+"\n");
	  //alert("onLocationChange host: "+aLocation.host+"\n");
        if (aLocation.host == 'oauthcallback.local') {
          var oauth_token = /oauth_token=([^&]*)/gi.exec(aLocation.spec)[1];
          //var oauth_verifier = /oauth_verifier=([^&]*)/gi.exec(aLocation.spec);
		  if (oauth_token) {
            //alert("***** oauth_token: "+oauth_token+"\n");
            doneAuthorizing(oauth_token);
          }
          /*if (oauth_verifier) {
            //alert("***** oauth_verifier: "+oauth_verifier[1]+"\n");
            doneAuthorizing(oauth_verifier[1]);
          }*/
        }
      },
  
    onStatusChange: function(/*in nsIWebProgress*/ aWebProgress,
                        /*in nsIRequest*/ aRequest,
                        /*in nsresult*/ aStatus,
                        /*in wstring*/ aMessage) {
      },
  
    onSecurityChange: function(/*in nsIWebProgress*/ aWebProgress,
                          /*in nsIRequest*/ aRequest,
                          /*in unsigned long*/ aState) {
      //alert("onSecurityChange: ["+aRequest.name+"] state "+aState+"\n");
      },
    onProgressChange64: function() {
      //alert("onProgressChange64: \n");
      },
    onRefreshAttempted: function() {
      //alert("onRefreshAttempted: \n");
      return true; }
  }


  function loadLoginFrame()
  {
    var nsIWebProgress = Components.interfaces.nsIWebProgress;
    var browser = document.getElementById("oauth_loginFrame");
    browser.addProgressListener(reporterListener, nsIWebProgress.NOTIFY_ALL);
    var url = window.arguments[0];
    if (url != "")
            browser.setAttribute("src", url);
  }